<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Matter extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $date=input();
        if($date){
            $where=[];
            $where['title']=$date['title'];
            $where['is_show']=$date['is_show'];
            $where['type']=1;
            $list=db('eb_special_task')->where($where)->paginate(2);
            $data=$list->items();
            $this->assign("data",$data);
            $this->assign("list",$list);
            return $this->fetch();
        }else{
            $where=[];
            $where['type']=1;
            $list=db('eb_special_task')->where($where)->paginate(2);
            $data=$list->items();
            $this->assign("data",$data);
            $this->assign("list",$list);
            return $this->fetch();
        }
       
    }
     public  function upload()
     {
         $file = request()->file('file');

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = '6IeVuqWlzQ9f3Vz19UVOAbDDxmwjZpZdf8VId5F_';
        $secretKey = '5T3TJ_O0x1rUOGtaw4kktV91nYZqe-NefC1dACk-';
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = '1810php';

        //域名
        $domain = 'qutzb4nvh.hb-bkt.clouddn.com';
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {

            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 0, 'data' => $imgPath, 'type' => 1, 'name' => $key]);

        }
       
     }
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data=input();
        $data['type']=1;
        $res=db('eb_special_task')->insert($data);
        if($res){
            $this->success("添加成功",url("Matter/index"));
        }else{
            $this->error("添加失败");
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $list=db('eb_special_task')->where('id',$id)->find();
        $this->assign("data",$list);
        return $this->fetch();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        $data=input();
        $res=db('eb_special_task')->where('id',$data['id'])->update($data);
        if($res){
            $this->success("编辑成功",url("Matter/index"));
        }else{
            $this->error("编辑失败");
        }
    }
    public function content($id)
    {
        if($this->request->isPost()){
            $data=input();
            $res=db('eb_special_task')->where('id',$data['id'])->update(["content"=>$data['content']]);
            if($res){
                $this->success("提交成功",url("Matter/index"));
            }else{
                $this->error("提交失败");
            }
        }else{
            $list=db('eb_special_task')->where('id',$id)->find();
            $this->assign("data",$list);
            return $this->fetch();
        }
      
    }
    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {
         if($this->request->isPost()){
            $data=input();
            $res=db('eb_special_task')->where('id',$data['id'])->update(["detail"=>$data['detail']]);
            if($res){
                $this->success("提交成功",url("Matter/index"));
            }else{
                $this->error("提交失败");
            }
        }else{
            $list=db('eb_special_task')->where('id',$id)->find();
            $this->assign("data",$list);
            return $this->fetch();
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function remove($id)
    {
         $res=db('eb_special_task')->where('id',$id)->delete();
        if($res){
            $this->success("删除成功",url("Matter/index"));
        }else{
            $this->error("删除失败");
        }
    }
}
